package com.punai.customer.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.customer.domain.CusImportPackage;
import com.punai.customer.domain.vo.CusSimplifyVo;
import com.punai.customer.mapper.CusImportDetailsMapper;
import com.punai.customer.mapper.CusImportPackageMapper;
import com.punai.customer.service.ICusImportPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 导入数据包Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-01
 */
@Service
public class CusImportPackageServiceImpl extends ServiceImpl<CusImportPackageMapper,CusImportPackage> implements ICusImportPackageService
{
    @Autowired
    private CusImportPackageMapper cusImportPackageMapper;
    @Autowired
    private CusImportDetailsMapper cusImportDetailsMapper;

    @Override
    public List<CusImportPackage> selectImportPackageList(CusImportPackage cusImportPackage) {
        List<CusImportPackage> cusImportPackageList = cusImportPackageMapper.selectCusImportPackageList(cusImportPackage);
        if(CollUtil.isNotEmpty(cusImportPackageList)){
            List<String> ids = cusImportPackageList.stream().map(CusImportPackage::getId).collect(Collectors.toList());
            List<CusSimplifyVo> cusSimplifyVos = cusImportDetailsMapper.selectContentByPackageIds(ids, cusImportPackage.getCompanyId());
            Map<String, String> idNameMap = cusSimplifyVos.stream().collect(Collectors.toMap(CusSimplifyVo::getId, CusSimplifyVo::getName));
            for (CusImportPackage importPackage : cusImportPackageList) {
                String content = idNameMap.get(importPackage.getId());
                importPackage.setDetailsList(Arrays.asList(content.split(",")));
            }
        }
        return cusImportPackageList;
    }
}
